<?php

namespace app\api\logic;

use app\common\exception\BusinessException;
use app\common\model\MpUser;

class Mp
{
    /**
     * @var
     */
    protected $appId;

    /**
     * $welcome
     * @var string
     */
    protected $welcome = '感谢关注，请回复数字选择服务';

    /**
     * selectMenu
     * @var string[]
     */
    protected $selectMenu = [
        '1. 搜题刷题小程序',
        '2. 查看绑定验证码',
        '3. 每日签到送积分',
        '4. 在线刷题网页版',
        '5. 高质量论文代写',
        '<a data-miniprogram-appid="wxbeb4d4de2823d484" data-miniprogram-path="pages/index/city/city">免费刷题小程序</a>',
    ];

    /**
     * @param mixed $appId
     */
    public function setAppId($appId): void
    {
        $this->appId = $appId;
        $this->initSelectMenu();
    }

    protected function initSelectMenu()
    {
        $selectMenu = [
            '1. 搜题刷题小程序',
            '2. 查看绑定验证码',
            '3. 每日签到送积分',
            '4. 在线刷题网页版',
            '5. 高质量论文代写'
        ];
        $selectMenu[] = '<a data-miniprogram-appid="' . $this->appId . '" data-miniprogram-path="pages/index/city/city">免费刷题小程序</a>';
        $this->selectMenu = $selectMenu;
    }

    /**
     * handler
     * @param $message
     * @return mixed
     */
    public function handler($message)
    {
        // 保存用户
        try {
            $fromUserName = $message['FromUserName'] ?? '';
            $this->saveUser($fromUserName);
            switch ($message['MsgType']) {
                case 'event';
                    return $this->eventHandler($message['Event']);
                    break;
                case 'text':
                    return $this->textHandler($message);
                    break;
                default:
                    return $this->getNotSupportText();
                    break;
            }
        } catch (\Exception $exception) {
            $content = '出现错误';
            $content .= ',错误信息:' . $exception->getMessage();
            $content .= ',错误文件:' . $exception->getFile();
            $content .= ',错误行号:' . $exception->getLine();
            return $content;
        }
    }

    /**
     * textHandler
     * @param $message
     * @return mixed
     */
    protected function textHandler($message)
    {
        // 提取参数
        $fromUserName = $message['FromUserName'];
        $content = trim($message['Content']);
        if (is_numeric($content)) {
            return (new MpMenu())->handler($message);
        }
        return $this->getNotSupportText();
    }

    /**
     * eventHandler
     * @param $messageEvent
     * @return string
     */
    protected function eventHandler($messageEvent): string
    {
        switch ($messageEvent) {
            case 'subscribe':
                return $this->getNotSupportText();
                break;
            default:
                return 'event 事件' . $messageEvent;
                break;
        }
    }

    /**
     * getMenuText
     * @return string
     */
    protected function getMenuText(): string
    {
        $text = '';
        foreach ($this->selectMenu as $key => $val) {
            $text .= $val . PHP_EOL;
        }
        return $text;
    }

    protected function getH5LinkText(): string
    {
        $name = '翰林刷题小程序';
        $value = '<a href="https://h5.5b1.cn">' . $name . '</a>';
        return $value;
    }

    /**
     * getMcpLinkText
     * @return string
     */
    protected function getMcpLinkText(): string
    {
        $name = '在线刷题小程序';
        $value = '<a data-miniprogram-appid="' . $this->appId . '" data-miniprogram-path="pages/index/city/city">' . $name . '</a>';
        return $value;
    }

    /**
     * getNotSupportText
     * @return string
     */
    protected function getNotSupportText(): string
    {
        return '请输入数字选择服务：' . PHP_EOL . $this->getMenuText();
    }

    /**
     * saveUser
     * @param $openId
     * @return void
     * @throws
     */
    protected function saveUser($openId)
    {
        $where = [['openid', '=', $openId]];
        $count = app(MpUser::class)->where($where)->count();
        if (!$count) {
            $time = time();
            $saveData = [
                'openid' => $openId,
                'create_time' => $time,
                'update_time' => $time,
            ];
            $isInsert = app(MpUser::class)->insert($saveData);
            if (!$isInsert) {
                throw new BusinessException('保存用户信息失败');
            }
        }
    }


}